// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Generate a random position in a marker
// -----------------------------------------------

private [
	"_i",
	"_entry",
	"_onmountain",
	"_onflat",
	"_onflatforbase",
	"_onroad",
	"_invillage",
	"_incity",
	"_incitycapital",
	"_onhill",
	"_onmount",
	"_onground",
	"_empty",
	"_xtemp",
	"_ytemp",
	"_nearestlocation",
	"_position",
	"_invalidate",
	"_isflat",
	"_gridofposition",
	"_countsleep",
	"_emptysizezone",
	"_newx",
	"_newy",
	"_count",
	"_markername",
	"_params",
	"_markersize",
	"_x",
	"_y"
];

_markername = _this select 0;

_params = [];

// Convert the strings to lower case; Add them to array
for [{_i = 0}, {_i < count _this}, {_i = _i + 1}] do {
	_entry = _this select _i;
	if (typeName _entry == "STRING") then {
		_entry = toLower _entry;
	};

	_params set [_i, _entry];
};

if ("onmountain" in _params) then {    _onmountain = true    } else { _onmountain = false };
if ("onflat" in _params) then {        _onflat = true        } else { _onflat = false };
if ("onflatforbase" in _params) then { _onflatforbase = true } else { _onflatforbase = false };
if ("onroad" in _params) then {        _onroad = true        } else { _onroad = false };
if ("invillage" in _params) then {     _invillage = true     } else { _invillage = false };
if ("incity" in _params) then {        _incity = true        } else { _incity = false };
if ("incitycapital" in _params) then { _incitycapital = true } else { _incitycapital = false };
if ("onhill" in _params) then {        _onhill = true        } else { _onhill = false };
if ("onmount" in _params) then {       _onmount = true       } else { _onmount = false };
if ("onground" in _params) then {      _onground = true      } else { _onground = false };
if ("empty" in _params) then {         _empty = true         } else { _empty = false };

_markersize = (getMarkerSize _markername) select 0;
_count = 0;
_countsleep = 0;
_emptysizezone = 10;

_position = [0,0,0];
_invalidate = true;
_x = abs((getMarkerPos _markername) select 0);
_y = abs((getMarkerPos _markername) select 1);

while {format ["%1", _position] == "[0,0,0]" or {_invalidate}} do {
	_position = [0,0,0];
	_invalidate = false;
	if (random 1 > 0.5) then {_xtemp = random _markersize} else {_xtemp = ((random _markersize) * -1)};
	if (random 1 > 0.5) then {_ytemp = random _markersize} else {_ytemp = ((random _markersize) * -1)};
	_newx = ceil(_xtemp + _x);
	_newy = ceil(_ytemp + _y);
	_position = [_newx, _newy];
	if (_invillage) then {
		_nearestlocation = nearestLocation [_position, "NameVillage"];
		_position = position _nearestlocation;
	};
	if (_incity) then {
		_nearestlocation = nearestLocation [_position, "NameCity"];
		_position = position _nearestlocation;
	};
	if (_incitycapital) then {
		_nearestlocation = nearestLocation [_position, "NameCityCapital"];
		_position = position _nearestlocation;
	};
	if (_onmountain) then {
		_position = [_position] call WC_fnc_getterraformvariance;
	};
	if (_empty) then {
		_position = _position findEmptyPosition [_emptysizezone, 300];
	};
	if (_onflat) then {
		_isflat = [];
		_isflat = _position isFlatEmpty [2, 0, 0.2, 2, 0, false];
		if (count _isflat == 0) then {
			_position = [0,0,0];
			_invalidate = true;
		};
	};
	if (_onflatforbase) then {
		_isflat = [];
		_isflat = _position isFlatEmpty [20, 0, 0.1, 20, 0, false];
		if (count _isflat < 1) then {
			_position = [0,0,0];
			_invalidate = true;
		};
	};
	if (_onroad) then {
		if (!isOnRoad _position) then {_position = [0,0,0]; _invalidate = true};
	} else {
		if (count (_position nearRoads 15) > 0) then {
			_position = [0,0,0];
			_invalidate = true;
		};
	};
	if (_onhill) then {
		_nearestlocation = nearestLocation [_position, "Hill"];
		_position = position _nearestlocation;
	};
	if (_onmount) then {
		_nearestlocation = nearestLocation [_position, "Mount"];
		_position = position _nearestlocation;
	};
	if (_onground) then {
		// Position can not be at less 50 meters of water
		_gridofposition = [_position, 50] call WC_fnc_creategridofposition;
		{
			if (surfaceIsWater _x) then {
				_position = [0,0,0];
				_invalidate = true;
			};
		} foreach _gridofposition;
	};

	_count = _count + 1;
	_countsleep = _countsleep + 1;

	if (_countsleep > 5) then {_countsleep = 0; sleep 0.5};
	if (_count > 50) then {_onflat = false; _emptysizezone = 3; diag_log "WARCONTEXT: POSITION LOOP EXIT OVER 50"};
	if (_count > 100) then {_position = [1,1,0]; _invalidate = false; diag_log "WARCONTEXT: POSITION LOOP EXIT OVER 100"};

	sleep 0.05;
};

_position;